package org.example.m6;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

public class Train {
    private String trainNumber;  // 车次号
    private String trainType;  // 列车类型
    private String departureStation;  // 出发站
    private String arrivalStation;  // 到达站
    private LocalDateTime departureTime;   // 出发时间
    private LocalDateTime arrivalTime;  // 到达时间

    public Train(String trainNumber, String trainType, String departureStation, String arrivalStation, LocalDateTime departureTime, LocalDateTime arrivalTime) {
        this.trainNumber = trainNumber;
        this.trainType = trainType;
        this.departureStation = departureStation;
        this.arrivalStation = arrivalStation;
        this.departureTime = departureTime;
        this.arrivalTime = arrivalTime;
    }

    public void show() {
        System.out.println(toString());
    }

    @Override
    public String toString() {
        return "Train{" +
                "trainNumber='" + trainNumber + '\'' +
                ", trainType='" + trainType + '\'' +
                ", departureStation='" + departureStation + '\'' +
                ", arrivalStation='" + arrivalStation + '\'' +
                ", departureTime=" + departureTime +
                ", arrivalTime=" + arrivalTime +
                '}';
    }

    public static void main(String args[]) {
        LocalDateTime departureTime = LocalDateTime.of(2023, 3, 15, 8, 0);
        LocalDateTime arrivalTime = LocalDateTime.of(2023, 3, 15, 14, 0);
        Train t1 = new Train("G123", "高速动车组", "西安北站", "上海虹桥站", departureTime, arrivalTime);
        t1.show();
    }
    static class SellTicket{
        static class Ticket {
            private double price;// 票价
            private int tickets; // 剩余票数

            public Ticket(double price, int tickets) {
                this.price = price;
                this.tickets = tickets;
            }

            public double getPrice() {
                return price;
            }

            public void setPrice(double price) {
                this.price = price;
            }

            public int getTickets() {
                return tickets;
            }

            public void setTickets(int tickets) {
                this.tickets = tickets;
            }

        }
        private Map<String, Ticket> tickets = new HashMap<>(); // 用来存储不同座位类型的票务信息
        public SellTicket(){}
        public SellTicket(String train,Ticket ticket){
            tickets.put(train,ticket);
        }
        public Boolean addTicket(String train,Ticket ticket){
            for (Map.Entry<String, Ticket> entry : tickets.entrySet()) {
                if (train.equals(entry.getKey())){
                    return false;
                }
            }
            tickets.put(train,ticket);
            return true;
        }

        public void sellTicket(String seatType) {
            Ticket ticket = tickets.get(seatType);
            if (ticket != null && ticket.getTickets() > 0) {
                ticket.setTickets(ticket.getTickets() - 1);
                System.out.println("成功售出一张" + seatType + "票，剩余票数：" + ticket.getTickets());
            } else {
                System.out.println(seatType + "票已售罄或不存在。");
            }
        }

        public void showTicketStatus() {
            for (Map.Entry<String, Ticket> entry : tickets.entrySet()) {
                System.out.println("车次" + entry.getKey() + ",剩余票数：" + entry.getValue().getTickets() + "");
            }
        }
    }
}
